package com.walle.userservice.controller;

import com.walle.common.entity.UserInfo;
import com.walle.common.result.ResultResponse;
import com.walle.userservice.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;

/**
 * @author zhangjiapeng
 * @Package com.walle.userservice.controller
 * @Description: ${todo}
 * @date 2018/12/26 16:21
 */
@RestController
@Slf4j
public class UserInfoController {


    @Autowired
    private UserInfoService userInfoService;


    @GetMapping("/login")
    public ResultResponse login(String username,String password){

        try {
            UserInfo userInfo = userInfoService.getByUserName(username);
            Assert.notNull(userInfo,"用户不存在");
            if(!userInfo.getPassword().equals(password)){
                return ResultResponse.ofError("账号或密码错误");
            }
            return  ResultResponse.ofSuccess();
        } catch (Exception e) {
           log.error("登录失败:{}",e.getMessage(),e);
           return ResultResponse.ofError(e.getMessage());
        }

    }

    @GetMapping("/register")
    public ResultResponse register(String username,String password){

        try {
            UserInfo userInfo = userInfoService.getByUserName(username);
            Assert.isNull(userInfo,"用户已存在");
            userInfo  = new UserInfo();
            userInfo.setUsername(username);
            userInfo.setPassword(password);
            userInfoService.insert(userInfo);
            return  ResultResponse.ofSuccess();
        } catch (Exception e) {
            log.error("登录失败:{}",e.getMessage(),e);
            return ResultResponse.ofError(e.getMessage());
        }

    }


    @GetMapping("/web")
    public String web(){
        return  "hello web";
    }

    @GetMapping("/android")
    public String android(){
        return  "hello android";
    }
}
